package com.service.admin.admin.controller;

import com.service.admin.admin.controller.request.MenuReq;
import com.service.admin.admin.controller.response.MenuResp;
import com.service.admin.admin.controller.response.UserMenuResp;
import com.service.admin.admin.service.SysMenuService;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.core.rest.response.OneResponse;
import com.service.dao.po.MenuItemPO;
import com.service.utility.common.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年07月27日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Api(tags = "权限相关")
@RestController
@RequestMapping("/sys-menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService menuService;

    @GetMapping("/detail/{id}")
    public OneResponse getMenuById(@PathVariable Integer id) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        MenuResp sysMenu = menuService.getMenuById(id);
        log.info("请求结束： UUID:{}", uuid);
        return successQueryOneResponse(sysMenu);
    }

    @GetMapping("/list-all")
    public MultiResponse listAllMenu() {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        List<MenuItemPO> sysMenu = menuService.listAllMenuTree();
        log.info("请求结束： UUID:{}", uuid);
        return successQueryMultiResponse(sysMenu);
    }

    @GetMapping("/init-user-menu")
    public MultiResponse initUserMenu() {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", "", uuid);
        List<UserMenuResp> sysMenu = menuService.initUserMenu();
        log.info("请求结束： UUID:{}", uuid);
        return successQueryMultiResponse(sysMenu);
    }

    @PostMapping("/save")
    public BaseResponse addMenu(@ApiParam @RequestBody @Validated MenuReq menuReq) {
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", menuReq, uuid);
        menuService.saveMenu(menuReq);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

    @PostMapping("/delete/{id}")
    public BaseResponse deleteMenu(@PathVariable Integer id){
        String uuid = UUIDUtil.randomUUID();
        log.info("请求开始 Msg: 请求参数：{}，UUID:{}", id, uuid);
        menuService.deleteMenu(id);
        log.info("请求结束： UUID:{}", uuid);
        return successSaveBaseResponse();
    }

}
